<?php
class Categoria_model extends CI_Model
{

    public function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }



    public function get_categoriaById($idCategoria = '0')
    {
        $sql = "" .
            "SELECT c.cod_categoria AS codigo, " .
            "       c.nom_categoria AS nombre, " .
            "       c.es_categoria  AS estado, " .
            "       c.orden_categoria  AS orden, " .
            "       IFNULL(TRIM(c.imagen_categoria), 'mininodisponible.png')  AS imagen, " .
            "       c.cod_categoria_padre  AS codigoCategoriaPadre, " .
            "       cp.nom_categoria  AS nombreCategoriaPadre " .
            "FROM   categoria c left join categoria cp on cp.cod_categoria=c.cod_categoria_padre " ;
            $sql.=" where	c.cod_categoria =". $idCategoria;

        $query = $this->db->query($sql);

        $result = array();
        foreach ($query->result_array() as $row)
        {
            $result[] = $row;
        }
        if(count($result)==0){
            return array();
        }
        return $result[0];
    }

    public function get_categorias($codCategoriaPadre='0')
    {
        $sql = "" .
            "SELECT c.cod_categoria AS codigo, " .
            "       c.nom_categoria AS nombre, " .
            "       c.es_categoria  AS estado, " .
            "       c.orden_categoria  AS orden, " .
            "       IFNULL(TRIM(c.imagen_categoria), 'mininodisponible.png')  AS imagen " .
            "FROM   categoria c " ;
            $sql.="WHERE  c.cod_categoria_padre = ".$codCategoriaPadre;


        $query = $this->db->query($sql);

        $result = array();
        foreach ($query->result_array() as $row)
        {
            $result[] = $row;
        }
        return $result;
    }

    public function get_subcategoriaById($idCategoria = '0')
    {
        $sql = "" .
            "SELECT c.cod_categoria AS codigo, " .
            "       c.nom_categoria AS nombre, " .
            "       c.es_categoria  AS estado, " .
            "       c.orden_categoria  AS orden, " .
            "       IFNULL(TRIM(c.imagen_categoria), 'mininodisponible.png')  AS imagen, " .
            "       c.cod_categoria_padre  AS codigoCategoriaPadre, " .
            "       'C'           AS tipo " .
            "FROM   categoria c " .
            "WHERE  c.cod_categoria_padre=" . $idCategoria . " order by  c.orden_categoria asc, c.nom_categoria asc ";
        $query = $this->db->query($sql);

        $result = array();
        foreach ($query->result_array() as $row)
        {
            $result[] = $row;
        }
        return $result;
    }


    public function get_links()
    {
        $sql = "" .
            "SELECT * " .
            "FROM   links order by orden " ;
        $query = $this->db->query($sql);

        $result = array();
        foreach ($query->result_array() as $row)
        {
            $result[] = $row;
        }
        return $result;
    }


    public function get_categoria($codCategoria = '0', $txt_buscar = '')
    {
        $sql = "" .
            "SELECT c.cod_categoria AS codigo, " .
            "       c.nom_categoria AS nombre, " .
            "       c.es_categoria  AS estado, " .
            "       c.cod_categoria_padre  AS codigoCategoriaPadre, " .
            "       'categoria'           AS tipo, " .
            "       c.orden_categoria   AS orden " .
            "FROM   categoria c " .
            "WHERE  1=1 ";
        if ($codCategoria != -1)
            $sql .=" and c.cod_categoria_padre=" . $codCategoria;

        if ($txt_buscar != '')
            $sql .= " and   c.nom_categoria like '" . $txt_buscar . "%' ";

        $sql .= " UNION " .
            "SELECT p.cod_producto AS codigo, " .
            "       p.nom_producto AS nombre, " .
            "       p.es_producto  AS estado, " .
            "       p.cod_categoria  AS codigoCategoriaPadre, " .
            "       'producto'           AS tipo, " .
            "       p.orden_producto as orden  " .
            "FROM   producto p where 1=1 " ;

        if ($codCategoria != -1)
            $sql .= "  and p.cod_categoria=" . $codCategoria;


        if ($txt_buscar != '')
            $sql .= " and   p.nom_producto like '" . $txt_buscar . "%' ";

        $sql .= " order by 6 asc ";

        $query = $this->db->query($sql);

        $result = array();
        foreach ($query->result_array() as $row)
        {
            $result[] = $row;
        }
        return $result;
    }
}

?>